﻿using System;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using System.IO;
using DNNToolkit.Modules.CityTaxLocator.Components;
using System.Web.UI.WebControls;

namespace DNNToolkit.Modules.CityTaxLocator
{
    /// <summary>
    /// The EditCityTaxLocator class is used to manage content.
    /// </summary>
    public partial class UploadData : PortalModuleBase
    {

        #region Event Handlers

        protected void mUpdateFileButton_Click(object sender, EventArgs e)
        {
            if (mDataFile.PostedFile.ContentLength > 0)
            {
                string filePath = Server.MapPath("./") + System.IO.Path.GetFileName(mDataFile.PostedFile.FileName);
                mDataFile.PostedFile.SaveAs(filePath);
                UpdateToDB(filePath);
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Update to DB.
        /// </summary>
        private void UpdateToDB(string filePath)
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open);
            try
            {
                CityTaxLocatorController controller = new CityTaxLocatorController();
                //prior
                int priorRecordCount = controller.GetCityTaxRecordCount();
                string messageText = string.Format(Localization.GetString("TotaleRecords.Prior", this.LocalResourceFile)
                    , priorRecordCount.ToString());
                ShowMessage(messageText);

                //update to database
                controller.UpdateCityTaxInfo(fileStream);
                ShowMessage(Localization.GetString("LoadingNewData.Text", this.LocalResourceFile));

                //after
                int afterRecordCount = controller.GetCityTaxRecordCount();
                messageText = string.Format(Localization.GetString("TotaleRecords.After", this.LocalResourceFile)
                    , afterRecordCount.ToString());
                ShowMessage(messageText);

                //finished
                ShowMessage(Localization.GetString("Finished.Text", this.LocalResourceFile));

                // show the return link
                HyperLink returnToSearchLink = new HyperLink();
                returnToSearchLink.Text = Localization.GetString("returnToSearchLink");
                returnToSearchLink.NavigateUrl = Globals.NavigateURL(this.TabId);
                this.Controls.Add(returnToSearchLink);
            }
            finally
            {
                fileStream.Close();
            }
        }

        /// <summary>
        /// Shows the message string.
        /// </summary>
        /// <param name="str">The STR value.</param>
        private void ShowMessage(string str)
        {
            Label lb = new Label();
            lb.CssClass = "Normal";
            lb.Text = str;
            this.Controls.Add(lb);            
        }
        #endregion
    }
}